<?php
require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_USER.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/Class_PWD.php' );
require_once( '../system/login.inc.php' );

Class_PWD::valid_auth(AOKI_AUTH_USER,$login['staff_auth']);

$dao = new Class_M_USER;

if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
	$phase = 'input';
	
	if( isset($_GET['id']) && $_GET['id'] != '' ){
		if( is_numeric( $_GET['id'] ) ){
			$forms  = $dao->get( $_GET['id'] );
		}
	}else{
		$forms['auth'] = "";
	}
}else{
	$forms = $_POST;
	if( $forms['mode'] == 'input' ){
		$err = new Class_ERROR;
		$chk = $dao->get_checks();
		
		$err_mes['login_id']	= $err->check( $forms['login_id'],  $chk['login_id'] );
		$err_mes['user_name']	= $err->check( $forms['user_name'], $chk['user_name'] );
		$err_mes['auth']		= $err->check( $forms['auth'],  $chk['auth']);
		$err_mes['email']		= $err->check( $forms['email'], $chk['email'] );
		if  ($forms['auth'] == 4) {
			$err_mes['ins_co_cd']		= $err->check( $forms['ins_co_cd'],  array( "EXIST" ) );
		}
		if  ($forms['auth'] == 9) {
			$err_mes['electric_id']		= $err->check( $forms['electric_id'],  array( "EXIST" ) );
		}
		if( $forms['id'] == '' ) $err_mes['login_pwd']	= $err->check( $forms['login_pwd'], $chk['login_pwd'] );
		
		if( $err_mes['login_id'] == '' ){
			if( $forms['id'] == '' ){
				$where = sprintf(" login_id = '%s'", $dao->esc( $forms['login_id'] ) );
			}else{
				$where = sprintf(" login_id = '%s' AND id <> %u", $dao->esc( $forms['login_id'] ), $dao->esc( $forms['id'] ) );
			}
			if ( $dao->exits($where) ) {
				$err->clear = false;
				$err_mes['login_id'] = '<p class="error">このIDは重複しています</p>';
			}
		}
		
		if ( $err->clear ){
			$values["login_id"]  =  $forms['login_id'] ;
			$values["auth"]      =  $forms['auth'] ;
			$values["ins_co_cd"]  =  $forms['ins_co_cd'] ;
			$values["electric_id"] =  empty($forms['electric_id']) ? 0 : $forms['electric_id'] ;

			$values["email"]     =  $forms['email'] ;
			$values["memo"]      =  $forms['memo'] ;
			$values["user_name"] =  $forms['user_name'];
			$values["login_pwd"] =  Class_PWD::do_encode( $forms['login_pwd'] ) ;
				
			if( $forms['id'] == '' ){
				$values["login_ng"]  =  "0" ;
			} else {
				$values["login_ng"]  =  $forms['login_ng'] ;
			}
			//桁数チェック
			$maxlens = $dao->get_maxlens();
			foreach ( $maxlens as $k => $v ) {
				$err_mes[$k] =$err->check_size($values[$k], $v);
				
			}
		}
		
		if( $err->clear ){
			if( $forms['id'] == '' ){
				if ($dao->add($values)) {
					$forms['id'] = $id;
					$smarty->assign('message', '登録が完了しました。');
				} else {
					$smarty->assign('message', '登録が失敗しました。');
				}
			}else{
				
				if ($dao->edit( $forms['id'], $values )) {
					$smarty->assign('message', '更新が完了しました。');
				} else {
					$smarty->assign('message', '更新が失敗しました。');
				}
				$id = $forms['id'];
			}
			$phase = 'complete';
		}else{
			$phase = 'input';
		}
	}
}
$select = array(''=>PLEASE_SELECT_ARR);
$arr_auth = array_merge($select, $arr_auth);

if( isset( $forms ) ) $smarty->assign('forms', $forms);
if( isset( $err_mes ) ) $smarty->assign('err', $err_mes);
$smarty->assign('auth', $arr_auth );
$ins_co_display = "none";
if  ($forms["auth"] == "4")  $ins_co_display = "block";
$smarty->assign('ins_co_display', $ins_co_display );

$ins_co = Class_M_OTHER::get_ins_co_array();
$ins_co = array_merge($select, $ins_co);
$smarty->assign('ins_co', $ins_co );

$electric_display = "none";
if  ($forms["auth"] == "9")  $electric_display = "block";
$smarty->assign('electric_display', $electric_display );
$electric = Class_M_OTHER::get_electric_array(PLEASE_SELECT_ARR);
$smarty->assign('electric', $electric );

$smarty->assign('phase', $phase );
$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );

$smarty->display('user_form.html');
?>